package org.dromara.wx.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wx.domain.bo.WxUserBo;
import org.dromara.wx.domain.query.WxUserQuery;
import org.dromara.wx.domain.vo.WxUserVo;
import org.dromara.wx.service.IWxUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * wx-微信用户
 *
 * @author yf
 * @date 2024-06-05
 */
@Validated
@RestController
@RequestMapping("/wx/user")
public class WxUserController extends BaseController {

    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询微信用户列表
     */
    @SaCheckPermission("wx:user:list")
    @GetMapping("/list")
    public TableDataInfo<WxUserVo> list(WxUserQuery query) {
        return wxUserService.queryPageList(query);
    }

    /**
     * 导出微信用户列表
     */
    @SaCheckPermission("wx:user:export")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxUserQuery query, HttpServletResponse response) {
        List<WxUserVo> list = wxUserService.queryList(query);
        ExcelUtil.exportExcel(list, "微信用户", WxUserVo.class, response);
    }

    /**
     * 获取微信用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission(value = {"wx:user:query", "wx:user:edit"}, mode = SaMode.OR)
    @GetMapping("/{id}")
    public R<WxUserVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(wxUserService.queryById(id));
    }

    /**
     * 新增微信用户
     */
    @SaCheckPermission("wx:user:add")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxUserBo bo) {
        return toAjax(wxUserService.insertByBo(bo));
    }

    /**
     * 修改微信用户
     */
    @SaCheckPermission("wx:user:edit")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxUserBo bo) {
        return toAjax(wxUserService.updateByBo(bo));
    }

    /**
     * 删除微信用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wx:user:remove")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] ids) {
        return toAjax(wxUserService.deleteWithValidByIds(List.of(ids)));
    }
}
